package vn.nad.aoeclips.screen.category.controller;

import java.util.ArrayList;

import vn.nad.android.aoeclips.R;
import vn.nad.aoeclips.common.model.entity.Article;
import vn.nad.aoeclips.common.utils.Constants;
import vn.nad.aoeclips.screen.category.controller.adapter.ArticleListAdapter;
import vn.nad.aoeclips.screen.category.controller.loader.DataArticleLoader;
import vn.nad.aoeclips.screen.detail.controller.DetailActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class CategoryFragment extends Fragment implements OnItemClickListener,
		LoaderCallbacks<ArrayList<Article>>, Constants {
	private ProgressDialog mProgressDialog;
	public static final String TAG = CategoryFragment.class.getSimpleName();

	private PullToRefreshListView listView;
	private int fragmentId = 0;

	private ArrayList<Article> listArticle = new ArrayList<Article>();
	private String categoryId;
	private ArticleListAdapter adapter;

	private boolean isForceReset = false;
	// admob widget
	private AdView adView;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_selected_category,
				container, false);
		return rootView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		listView = (PullToRefreshListView) view
				.findViewById(R.id.pull_to_refresh_listview);
		// Create an ad.
		adView = (AdView) view.findViewById(R.id.adView);
		// adView.setAdSize(AdSize.SMART_BANNER);
		// adView.setAdUnitId(AD_UNIT_ID);
		// Create an ad request. Check logcat output for the hashed device ID to
		// get test ads on a physical device.
		AdRequest adRequest = new AdRequest.Builder().build();

		// Start loading the ad in the background.
		adView.loadAd(adRequest);
		adView.setAdListener(new AdListener() {

		});
		adapter = new ArticleListAdapter(getActivity(), listArticle);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		initData();
	}

	public void initData() {
		initmProgressDialog();
		Bundle bundle = getArguments();
		categoryId = bundle.getString(ARGUMENT_CATEGORY_ID);
		fragmentId = bundle.getInt(ARGUMENT_FRAGMENT_ID, 0);

		if (listArticle.size() == 0 || isForceReset) {
			LoaderManager lm = getActivity().getSupportLoaderManager();
			lm.destroyLoader(fragmentId);
			lm.initLoader(fragmentId, null, this);
		}
	}

	/**
	 * loader
	 */
	@Override
	public Loader<ArrayList<Article>> onCreateLoader(int arg0, Bundle arg1) {
		if (mProgressDialog != null) {
			mProgressDialog.show();
		}
		return new DataArticleLoader(getActivity(), categoryId);
	}

	@Override
	public void onLoadFinished(Loader<ArrayList<Article>> loader,
			ArrayList<Article> data) {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
		listArticle.clear();
		listArticle.addAll(data);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onLoaderReset(Loader<ArrayList<Article>> loader) {

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = new Intent(getActivity(), DetailActivity.class);
		intent.putParcelableArrayListExtra(ARGUMENT_ARTICLES, listArticle);
		intent.putExtra(ARGUMENT_POSITION, position);
		// intent.putExtra(ARGUMENT_ARTICLES,
		// listArticle.get(position).getLink());
		startActivity(intent);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (adView != null) {
			adView.resume();
		}
	}

	@Override
	public void onPause() {
		if (adView != null) {
			adView.pause();
		}
		super.onPause();
	}

	/** Called before the activity is destroyed. */
	@Override
	public void onDestroy() {
		// Destroy the AdView.
		if (adView != null) {
			adView.destroy();
		}
		super.onDestroy();
	}

	private void initmProgressDialog() {
		mProgressDialog = new ProgressDialog(getActivity());
		mProgressDialog.setTitle(R.string.app_name);
		mProgressDialog.setMessage(getString(R.string.loading));
		mProgressDialog.setCancelable(false);
		mProgressDialog.setIndeterminate(true);
	}

}
